/**
 * @author wangsg
 */
var _userinfo = null;
$(function() {
	//居中
	window.nativeWindow.x = (air.Capabilities.screenResolutionX - Const.msg._width) / 2;
	window.nativeWindow.y = (air.Capabilities.screenResolutionY - Const.msg._height ) / 2;

	//启用拖拽
	Common.DragStart($(".msg_header")[0]);

	//最小化和关闭
	$(".msg_header .min").bind("click", function(event) {
		event.stopPropagation();
		window.nativeWindow.minimize();
	});
	$(".msg_header .close").bind("click", function(event) {
		event.stopPropagation();
		window.nativeWindow.close();
	});
	//自适应大小

	var height = Const.msg._height - 198;
	$(".msg_body").height(height);

	//改变大小
	$(".resizebar").bind("mousedown", function() {
		window.nativeWindow.startResize("BR");
	});
	/********************************************************/
	//调整窗口大小时处理界面
	$(window).resize(function() {
		var nwHeight = window.nativeWindow.height;
		var height = nwHeight - 198;
		$(".msg_body").height(height);
	});
	var config = App.getConfig();
	//发送菜单
	var menu = new air.NativeMenu();
	//Enter发送控制
	var enterItem = new air.NativeMenuItem("Enter发送");
	var ctrlEnterItem = new air.NativeMenuItem("Ctrl+Enter发送");
	if(config.MsgConfirm == 0) {
		enterItem.checked = true;
		ctrlEnterItem.checked = false;
	} else {
		enterItem.checked = false;
		ctrlEnterItem.checked = true;
	}
	var enterCommand = menu.addItem(enterItem);
	enterCommand.addEventListener(air.Event.SELECT, function(event) {
		config.MsgConfirm = 0;
		App.setConfig(config);
		enterItem.checked = true;
		ctrlEnterItem.checked = false;
	});
	var ctrlEnterCommand = menu.addItem(ctrlEnterItem);
	ctrlEnterCommand.addEventListener(air.Event.SELECT, function(event) {
		config.MsgConfirm = 1;
		App.setConfig(config);
		ctrlEnterItem.checked = true;
		enterItem.checked = false;
	});
	//调用菜单
	$(".msg_enterbtn").bind("click", function(event) {
		menu.display(window.nativeWindow.stage, event.clientX, event.clientY);
	});
	//初始化数据
	$("#msgText").focus();
	ShowLoading();
	var tid = App.getTempData();
	App.getSendToUsers(tid, function(data) {
		var flag = tid.split("_")[0];
		if(flag == "user") {
			$(".title").html(data.userName);
			_userinfo = data;
			var pid = tid.split("_")[1];
			var file = new air.File(air.File.applicationDirectory.resolvePath("app:/assets/images/photos/" + pid + ".jpg").nativePath);
			if(file.exists) {
				$(".photoimg").attr("src", "assets/images/photos/" + pid + ".jpg");
			}
			$(".msg_btn :checkbox").attr("disabled", false);
		} else if(flag == "dept") {
			//隐藏个人信息
			$(".msg_headerphoto").hide();
			$(".title").css({
				"color" : "#fff",
				"font-weight" : "bold",
				"left" : "10px"
			});
			$(".msg_body").html(data);
			var idListStr = GetUserIDList();
			var uCount = idListStr.split(",").length;
			if(idListStr == "")
				uCount = 0;
			$(".title").html("发寻呼到" + uCount + "人");
			$(".msg_btn :checkbox").attr("disabled", false);
		} else if(flag == "tid") {
			//隐藏个人信息
			$(".msg_headerphoto").hide();
			$(".title").css({
				"color" : "#fff",
				"font-weight" : "bold",
				"left" : "10px"
			});
			$(".title").html("回复寻呼");
			$(".msg_btn :checkbox").attr("disabled", true);
			App.getCallContent(tid.split("_")[1], function(data) {
				var callstr = "";
				var time = "";
				for(var i = data.messages.length - 1; i >= 0; i--) {
					time = data.messages[i].time.split("-")[1] + "-" + data.messages[i].time.split("-")[2];
					time = time.split(":")[0] + ":" + time.split(":")[1];
					time = data.messages[i].sender + "&nbsp;" + time;
					callstr += App.createMsgLeftNode(data.messages[i].content, time);
				}
				$(".msg_body").html(callstr);
			}, function(err) {
				Common.ShowTips(err);
			});
		}
		_userinfo = {};
		_userinfo.userID = tid;
		HideLoading();
	}, function(err) {
		Common.ShowTips(err);
	});
	//快捷键
	$("#msgText").bind("keydown", function(event) {
		if(event.keyCode == 13) {
			var config = App.getConfig();
			if(config.MsgConfirm == 0) {
				$("#btnSend").trigger("click");
				return false;
			}
		}
		if(event.keyCode == 13 && event.ctrlKey) {
			$("#btnSend").trigger("click");
			return false;
		}
	});
	//寻呼发送
	$("#btnSend").bind("click", function() {
		var msgContent = "";
		msgContent = $("#msgText").val();
		if(msgContent.trim() == "") {
			Common.ShowTips("寻呼不能为空");
			return;
		}
		if(msgContent.length > 4000) {
			Common.ShowTips("寻呼内容过长");
			return;
		}

		if(_userinfo.userID.split("_")[0] == "tid") {
			var msg = {};
			msg.replyId = _userinfo.userID.split("_")[1];
			msg.content = msgContent;
			App.replyUnreadMessage(msg, function() {
				var time = Common.GetTime("smalltime");
				$(".msg_body").append(App.createMsgRightNode(msgContent, time));
			}, function(data) {
				Common.ShowTips(data);
			});
		} else {
			var userids = "";
			userids = GetUserIDList();
			if(userids.trim() == "") {
				Common.ShowTips("请选择寻呼接受人");
				return;
			}
			ShowLoading();
			var Msg = {};
			Msg.strSendContent = msgContent;
			Msg.strSendToUsers = userids;
			if($(".msg_btn :checkbox")[0].checked)
				Msg.strSmsSign = "1";
			else
				Msg.strSmsSign = "0";
			Msg.strMobileNum = "";
			App.sendMsg(Msg, function() {
				HideLoading();
				if(userids.indexOf(",") > 0) {
					Common.ShowTips("发送成功");
				} else {
					var time = Common.GetTime("smalltime");
					$(".msg_body").append(App.createMsgRightNode(msgContent, time));
				}
			}, function(data) {
				Common.ShowTips(data);
				HideLoading();
			});
		}
		$("#msgText").val("");
	});
	//移除人员
	$(".usercard_s .close").live("click", function() {
		$(this).parent().hide(200, function() {
			var idListStr = GetUserIDList();
			var uCount = idListStr.split(",").length;
			if(idListStr == "")
				uCount = 0;
			$(".title").html("发寻呼到" + uCount + "人");
		});
	});
	//添加接收人
	$(".msg_target .subbtn").live("click", function() {
		//打开人员选择界面，隐藏本界面
		if(_userinfo.userID.split("_")[0] == "tid") {
			Const.userlist._idlist = "";

		} else {
			Const.userlist._idlist = GetUserIDList();
		}

		Common.OpenUserList();
		//窗口再此打开时
		window.nativeWindow.addEventListener(air.Event.ACTIVATE, function() {
			var userList = App.getTempData();
			var userNameList = userList.split("|")[1].split(",");
			var userIDList = userList.split("|")[0].split(",");
			var usersinfo = "";
			if(userList != "") {
				if(userList.split("|")[0].trim() == "")
					return;
				if(userIDList.length == 1) {
					//显示个人信息
					$(".msg_headerphoto").show();
					$(".title").css({
						"color" : "#DBE7EB",
						"font-weight" : "normal",
						"left" : "70px"
					});
					$(".msg_body").html("");
					var tid = "user_" + userIDList[0];
					App.getSendToUsers(tid, function(data) {
						$(".title").html(data.userName);
						_userinfo = data;
						var pid = tid.split("_")[1];
						var file = new air.File(air.File.applicationDirectory.resolvePath("app:/assets/images/photos/" + pid + ".jpg").nativePath);
						if(file.exists) {
							$(".photoimg").attr("src", "assets/images/photos/" + pid + ".jpg");
						}
						_userinfo.userID = tid;
					}, function(err) {
						Common.ShowTips(err);
					});
					_userinfo.userID = tid;
				} else {
					//隐藏个人信息
					$(".msg_headerphoto").hide();
					$(".title").css({
						"color" : "#fff",
						"font-weight" : "bold",
						"left" : "10px"
					});
					$(".title").html("发寻呼到" + userIDList.length + "人");

					for(var k = 0; k < userIDList.length; k++) {
						usersinfo += "<div id='user_" + userIDList[k] + "' class=\"usercard_s\"><span>" + userNameList[k] + "</span><span class=\"close\">&nbsp;&nbsp;&nbsp;</span></div>";
					}
					$(".msg_body").html(usersinfo);
					_userinfo.userID = "dept_0";
				}
			}
		});
	});
});
function ShowLoading() {
	$(".msg_loading").height(window.nativeWindow.height - 61);
	$(".msg_loading").show();
}

function HideLoading() {
	$(".msg_loading").hide();
}

//获取当前选择寻呼接收人ID
function GetUserIDList() {
	var tid = "";
	var flag = "dept";
	if(_userinfo) {
		tid = _userinfo.userID;
		flag = tid.split("_")[0];
	}
	if(flag == "user" || flag == "tid") {
		return tid.split("_")[1];
	} else if(flag == "dept") {
		var idlist = "";
		$(".usercard_s").each(function() {
			if($(this).is(":visible")) {
				idlist += this.id.split("_")[1] + ",";
			}
		});
		if(idlist.indexOf(",") > 0) {
			idlist = idlist.substr(0, idlist.length - 1);
		}
		return idlist;
	}
}